package com.test.reggieadmin.model.category;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;

import com.test.reggieadmin.R;
import com.test.reggieadmin.base.BaseActivity;
import com.test.reggieadmin.bean.BaseBean;
import com.test.reggieadmin.bean.CategoryDetailsBean;
import com.test.reggieadmin.bean.EmployeeBean;
import com.test.reggieadmin.bean.State;
import com.test.reggieadmin.databinding.ActivityAddCategoryBinding;
import com.test.reggieadmin.http.API;
import com.yechaoa.yutils.GsonUtil;
import com.yechaoa.yutils.ToastUtil;
import com.yechaoa.yutils.YUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 添加分类
 */
public class AddCategoryActivity extends BaseActivity<ActivityAddCategoryBinding, AddCategoryPrensenter>
        implements AddCategoryView, View.OnClickListener {

    private EmployeeBean employeeBean;
    private int type = 1;
    private String id;

    @Override
    protected AddCategoryPrensenter createPresenter() {
        return new AddCategoryPrensenter(this);
    }

    @Override
    protected void initView() {
        viewBinding.icHead.tvTitle.setText("添加分类");
        viewBinding.icHead.llBack.setOnClickListener(this);
        viewBinding.btnSave.setOnClickListener(this);
        employeeBean = GsonUtil.GsonToBean(String.valueOf(API.kv.decodeString(State.employee)), EmployeeBean.class);

        id = getIntent().getStringExtra("id");
        if (id != null && !"".equals(id)) {
            viewBinding.icHead.tvTitle.setText("修改分类");
            YUtils.showLoading("加载中...");
            presenter.categoryDetails(id);//根据id查询分类
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_back:
                finish();
                break;
            case R.id.btn_save:
                addCategory();//添加分类
                break;
            default:
                break;
        }
    }

    private void addCategory() {
        if (TextUtils.isEmpty(viewBinding.etName.getText().toString().trim())) {
            ToastUtil.showCenter("分类名称不能为空");
        } else if (TextUtils.isEmpty(viewBinding.etSort.getText().toString().trim())) {
            ToastUtil.showCenter("排序不能为空");
        } else {
            YUtils.showLoading("提交中...");
            Map<String, Object> mapEmployee = new HashMap<>();
            mapEmployee.put("updateUser", employeeBean.getData().getUsername());
            mapEmployee.put("name", viewBinding.etName.getText().toString().trim());
            mapEmployee.put("type", type);
            mapEmployee.put("sort", viewBinding.etSort.getText().toString().trim());
            if (id != null && !"".equals(id)) {
                YUtils.showLoading("提交中...");
                mapEmployee.put("id", id);
                presenter.updateCategory(mapEmployee);//修改分类
            } else {
                mapEmployee.put("createUser", employeeBean.getData().getUsername());
                presenter.addCategory(mapEmployee);//添加分类
            }
        }
    }

    @Override
    protected void initData() {
        viewBinding.rg.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == R.id.rb1) {
                type = 1;
            } else if (checkedId == R.id.rb2) {
                type = 2;
            }
        });
    }

    @Override
    public void addCategorySuccess(BaseBean bean) {
        YUtils.hideLoading();
        ToastUtil.showCenter(bean.getMsg());
        if (bean.getCode() == 200) {
            setResult(Activity.RESULT_OK);
            finish();
        }
    }

    @Override
    public void onError(String msg) {
        YUtils.hideLoading();
        ToastUtil.showCenter(msg);
    }

    @Override
    public void categoryDetailsSuccess(CategoryDetailsBean bean) {
        YUtils.hideLoading();
        if (bean.getCode() == 200) {
            viewBinding.etName.setText(bean.getData().getName());
            type = bean.getData().getType();
            if (type == 1) {
                viewBinding.rb1.setChecked(true);
            } else {
                viewBinding.rb2.setChecked(true);
            }
            viewBinding.etSort.setText("" + bean.getData().getSort());
        }
    }

    @Override
    public void updateCategorySuccess(BaseBean bean) {
        YUtils.hideLoading();
        ToastUtil.showCenter(bean.getMsg());
        if (bean.getCode() == 200) {
            setResult(Activity.RESULT_OK);
            finish();
        }
    }
}